package com.netty.demo;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: Admin
 * @Date: 2021/4/14 13:14
 * @Description:
 */
public class TestWalkFileTree {


    public static void main(String[] args) throws Exception {

//        m1();
//        m2();
        m3();

    }


    // 级联copy
    private static void m3() throws IOException{

        String source = "D:\\个人";
        String target = "D:\\个人_cogy";

        Files.walk(Paths.get(source)).forEach(path -> {


            String targetName = path.toString().replace(source, target);

            if(Files.isDirectory(path)){// 先创建目录

                try {
                    Files.createDirectories(Paths.get(targetName));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            if(Files.isRegularFile(path)){// 在创建文件

                try {
                    Files.copy(path, Paths.get(targetName));
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }


        });


    }


    // 级联删除操作
    private static void m2() throws Exception{


        Files.walkFileTree(Paths.get("D:\\java_home\\jdk1.8.0_191_test"), new SimpleFileVisitor<Path>(){


            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {// 先执行

                Files.delete(file);

                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {// 后执行

                Files.delete(dir);

                return super.postVisitDirectory(dir, exc);
            }
        });



    }


    // 遍历操作
    private static void m1() throws IOException {
        System.out.println(System.getenv().get("JAVA_HOME"));
        System.out.println(System.getProperty("user.dir"));

        AtomicInteger fileCount = new AtomicInteger();
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger jarCount = new AtomicInteger();


        Files.walkFileTree(Paths.get("D:\\java_home\\jdk1.8.0_191"), new SimpleFileVisitor<Path>(){

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

                System.out.println(dir);

                fileCount.incrementAndGet();

                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {


                System.out.println(file);

                if(file.getFileName().endsWith(".jar")){

                    jarCount.incrementAndGet();

                }

                dirCount.incrementAndGet();

                return super.visitFile(file, attrs);
            }
        });

        System.out.println(fileCount);
        System.out.println(dirCount);
    }


}
